package org;

import org.entities.LevelObject;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

public class IntroState  extends BasicGameState {

    public static final float MOVE_SPEED = 0.001f;
    private float dx = 0;
    private String msg1,msg2,msg3,msg4,fbstring,tempo;
    private StateBasedGame game;
    private Image space1,space2, space3, defImg;
    public static final int ID = 7;	
    public static boolean intro=true;
    private Font font;
    private Music title;
    private boolean musicControl,startGame;
    public Animation ship,shipBack,fallback; 
    private int currentStringTime = 0;    
    private int maxStringTime = 100; 
    private int stringTimer,stringCounter;
    float scaleX=1;
    float scaleY=1;
    int x=100;
    int y=30;
    private Sound zebes; //,typing;
    float scale=1;
    
    public int getID() {
        return ID;
    }

    public void init(GameContainer container, StateBasedGame game) throws SlickException {

        this.game = game;		
        space1 = new Image("data/image/intro2.png");
        space2 = new Image("data/image/intro1.png");
        space3 = new Image("data/image/intro4.png");
        font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");

        SpriteSheet sheet = new SpriteSheet("data/anim/smallShip.png", 80, 32);
        ship = new Animation();
        for (int frame = 0; frame < 2; frame++) {
            ship.addFrame(sheet.getSprite(frame, 0),100);
        }
        SpriteSheet sheet2 = new SpriteSheet("data/anim/smallShip2.png", 64,32);
        shipBack = new Animation();
        for (int frame = 0; frame < 2; frame++) {
            shipBack.addFrame(sheet2.getSprite(frame, 0), 20);
        }
        ship.setAutoUpdate(false);
        shipBack.setAutoUpdate(false);

        msg1 = "En route to home, my ship sustained\n" +
        "damage from an electro-magnetic storm .."+
        "             ";
        msg2 = "I needed to do some repairs before i could \n"+ 
        "continue my journey home..            \n" + 
        "Fortunately, the onboard computer 'gaia' detected a";
        msg3 = "space colony within reasonable range.\n" + 
        "..This was my chance, i thought.. \n"+
        "maybe someone there would be able to assist me"+
        "                "; 
        msg4 = "         ";
        title=new Music("data/music/Intro2.ogg"); 
        zebes = new Sound("data/sound/SpaceShip2.ogg");
        //typing = new Sound("data/sound/typing.ogg");
        fbstring=msg1;
        stringCounter=1;
        fallback=ship;
        defImg=space1;
    }

    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {

        //startGame=true; //skip intro part 1 
        Progress.showIntro=false;
        
        if (!startGame){
            dx += (float) delta * MOVE_SPEED*20;            
            fallback.update(delta);            
            if (!musicControl){
                musicControl=true;
                title.setVolume(Progress.musicVol/20);
                title.play();
            }
            if (dx>400){
                defImg =space2;
                dx=0;
            }
            if (currentStringTime < maxStringTime) {
                currentStringTime += delta;
            }
            if (currentStringTime >= maxStringTime) {            
                if (stringTimer<fbstring.length()){
                    stringTimer+=1;  
                    tempo= fbstring.substring(0, stringTimer);
                    //if (!(typing.playing()))
                    //    typing.play();
                }
                if (stringTimer==fbstring.length()){
                    stringTimer=0;
                    stringCounter+=1;

                    if (stringCounter==2)
                        fbstring=msg2;
                    if (stringCounter==3)
                        fbstring=msg3;
                    if (stringCounter==4){
                        fbstring=msg4; 
                        startGame=true;
                        musicControl=false;
                    }
                }
                currentStringTime = 0;
            }
        }
        
        if (startGame){            
            if (!musicControl){
                musicControl=true;
                zebes.play();
            }            
            fallback=shipBack;
            fallback.update(delta);
            title.stop();
            scale-=0.00008f*delta;
            //System.out.printf("scale=[%f]\n",scale);
        }

        if (scale <= 0.16){
            zebes.stop();
            game.enterState(GameState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));            
        }
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) {
        if (!LevelObject.debugMode)
        g.scale(2f, 2f);
        if (!startGame){
            g.drawImage(defImg,0,40);
            g.setFont(font);
            g.setColor(Color.white);
            g.drawAnimation(fallback, dx, 135);
            if (tempo!=null)
                g.drawString(tempo, 20, 240);

        }
        if (startGame){

            g.drawImage(space3,0,40);
            //g.translate(x, y);
            g.scale(scale, scale);            
            // do zoom without moving:
            // g.drawAnimation(fallback, -fallback.getWidth()/2f, -fallback.getHeight()/2f);
            // do zoom while moving:
            // g.drawAnimation(fallback, x - fallback.getWidth()/2f, y- fallback.getHeight()/2f);
            // acceptable:
            // g.drawAnimation(fallback,1800-scale*2200 ,1000-scale*1200);
            // another one:
            g.drawAnimation(fallback,1200-scale*1200 ,1000-scale*1200);            
            g.resetTransform(); 
            
            if (!LevelObject.debugMode)
            g.scale(2f, 2f);
            g.setColor(Color.white);
            g.drawString("Planet: Zebes", 150, 240);
        }
    }

    public void keyReleased(int key, char c) {
        if (key == Input.KEY_ESCAPE) {
            title.stop();
            game.enterState(GameState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
        }
    }
}
